<?php
/**
 * 方便等级为主管的人能查看下级申请
 */
apf_require_class("Bll_BLL");

class Bll_Flow_ApplyRelation extends BLL {

    /**
     * 当员工申请时给相关主管与入关系，方便上级查看
     * @param int $flow_id
     * @param int $user_id
     * @param int $parent_id
     * @return boolean
     */
    public function insert($flow_id, $user_id, $parent_id,$flow_stage = 0){
        if(!$flow_id || !$user_id || !$parent_id){
            return false;
        }
        $data = array(
                'flow_id'=>intval($flow_id),
                'user_id'=>intval($user_id),
                'parent_uid'=>intval($parent_id),
                'flow_stage'=>intval($flow_stage)
        );
        return $this->get_apply_dao()->insert($data);
    }
    /**
     * 获取当前主管能看到的下级人员待办事项
     * @param int $parent_id
     * @return array
     */
    public function get_sub_user($parent_id){
        if(!$parent_id){
            return array();
        }
        $where = array(
               'parent_uid'=>intval($parent_id),
               'is_deleted'=>1
                );
        return $this->get_apply_dao()->find($where,'',0);
    }
    /**
     * 获得该阶段的上级ID
     * @param unknown_type $params
     * @return multitype:
     */
    public function get_relation($flow_id,$params){
        if(!$flow_id || !$params){
            return array();
        }
        $where = array(
                'flow_id'=>intval($flow_id)
        );
        if($params['user_id']){
            $where['parent_uid'] = intval($params['user_id']);
        }
        if($params['flow_stage']){
            $where['flow_stage'] = intval($params['flow_stage']);
        }
        if(!empty($params['is_deleted'])){
            $where['is_deleted'] = intval($params['is_deleted']);
        }
        return $this->get_apply_dao()->find_row($where,'',0);
    }
    /**
     *
     * @param int $flow_id
     */
    public function update_by_flow($flow_id){
        if(!$flow_id){
            return false;
        }
        return $this->get_apply_dao()->update(array('is_deleted'=>2),array('flow_id'=>$flow_id));
    }
    /**
     * 统计当前还有效的审批关系
     * @param int $uid
     */
    public function is_super_count($uid){
        if(!$uid){
            return 0;
        }
        $where = array(
                'parent_uid'=>intval($uid),
                'is_deleted'=>1
        );
        return $this->get_apply_dao()->find_count($where);
    }
    /**
     *
     */
    private function get_apply_dao(){
        $dao = $this->get_dao('Dao_Flow_ApplyRelation');
        return $dao;
    }
}